package com.cenntro.oss.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.cenntro.common.base.BaseController;
import com.cenntro.common.util.result.Result;
import com.cenntro.oss.service.ExcelUploadService;

import lombok.extern.log4j.Log4j2;

/**
 * 
* @ClassName: ExcelUploadController
* @Description: 车辆数据导入
* @author 15815
* @date 2019年7月3日
*
 */
@RestController
@RequestMapping("/excel")
@Log4j2
public class ExcelUploadController extends BaseController {

	@Resource
	private ExcelUploadService excelUploadService;
	/**
	 * 
	* @Title importData
	* @Description 导入车辆数据
	* @param str
    * @param type 
	* @return  
	* @throws
	 */
	@RequestMapping("/importData")
	@CrossOrigin
	public Result importData(String str, Integer type) {
//		log.info("接收的str：{},type：{}", str, type);
		long start = System.currentTimeMillis();
		if (str != null && str.length() > 5) {
			JSONArray arrayList = JSONArray.parseArray(str);
			String jsonString = arrayList.toJSONString();
			if (type == 1) {// 车辆基本信息
				Result res = excelUploadService.importDevice(jsonString);
				log.info("基本数据导入耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 2) {// 零部件信息
				Result res = excelUploadService.importDeviceParts(jsonString);
				log.info("零部件信息导入耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 3) {// 合格证信息
				Result res = excelUploadService.importCertificate(jsonString);
				log.info("合格证导入耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 4) {// 电池包信息
				Result res = excelUploadService.importBatteryPack(jsonString);
				log.info("电池包导入耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 5) {// 电池模块信息
				Result res = excelUploadService.importBatteryModule(jsonString);
				log.info("电池模块导入耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 6) {// 单体电池信息
				Result res = excelUploadService.importBattery(jsonString);
				log.info("单体电池导入耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else {
				return Result.error("无此类型type");
			}
		}
		return Result.error("表格内无数据，请检查后重新导入");
	}

	/**
	 * 
	* @Title updateData
	* @Description 更新车辆数据
	* @param type
	* @param str
	* @return  
	* @throws
	 */
	@RequestMapping("/updateData")
	@CrossOrigin
	public Result updateData(String str, Integer type) {
//		log.info("接收的str：{},type：{}", str, type);
		long start = System.currentTimeMillis();
		if (str != null && str.length() > 5) {
			JSONArray arrayList = JSONArray.parseArray(str);
			String jsonString = arrayList.toJSONString();
			if (type == 1) {// 车辆基本信息
				Result res = excelUploadService.updateDevice(jsonString);
				log.info("基本数据更新耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 2) {// 零部件信息
				Result res = excelUploadService.updateDeviceParts(jsonString);
				log.info("零部件信息更新耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 3) {// 合格证信息
				Result res = excelUploadService.updateCertificate(jsonString);
				log.info("合格证更新耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 4) {// 电池包信息
				Result res = excelUploadService.updateBatteryPack(jsonString);
				log.info("电池包更新耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 5) {// 电池模块信息
				Result res = excelUploadService.updateBatteryModule(jsonString);
				log.info("电池模块更新耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else if (type == 6) {// 单体电池信息
				Result res = excelUploadService.updateBattery(jsonString);
				log.info("单体电池更新耗时：{}ms", (System.currentTimeMillis() - start));
				return res;
			} else {

				return Result.error("无此类型type");
			}
		}
		return Result.error("表格内无数据，请检查后重新导入");
	}
	
}
